package com.example.administrator.wallet.adapter;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.wallet.R;
import com.example.administrator.wallet.constant.Api;
import com.example.administrator.wallet.intf.IRequestCallback;
import com.example.administrator.wallet.intf.IRequestManager;
import com.example.administrator.wallet.model.bean.TokenBean;
import com.example.administrator.wallet.utils.RequestFactory;
import com.example.administrator.wallet.utils.SharedPrefsUtil;
import com.example.administrator.wallet.view.activity.find.OffSiteTransactions.OffSiteTransactionsActivity;
import com.example.administrator.wallet.view.activity.find.cointrans.CoinTransactionActivity;
import com.example.administrator.wallet.view.activity.finds.kkbank.KKBankActivity;
import com.example.administrator.wallet.view.activity.finds.kkbank.SetTradePasswordActivity;
import com.example.administrator.wallet.view.activity.mine.Personal.ChangNumber.ChangNumberActivity;
import com.example.administrator.wallet.view.activity.mine.Personal.RealAuthentication.RealAuthenticationAcitivity;
import com.google.gson.Gson;

import java.util.List;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

/**
 * Effect:
 * <p>
 * Author:  CaiYapeng
 * <p>
 * Time  :  2018/8/16.
 */
public class FindListItemAdapter extends BaseAdapter {
    private Activity context;
    private List<Integer> item_one_icon;
    private List<String> item_one_title;

    ViewHolder holder;
    private String photo = "";
    private int verifiedstatus = 0;
    private int exchange_password = 0;
    final IRequestManager reqequestManager = RequestFactory.getReqequestManager();
    private TokenBean tokenBean;


    public FindListItemAdapter(Activity context, List<Integer> item_one_icon, List<String> item_one_title) {
        this.context = context;
        this.item_one_icon = item_one_icon;
        this.item_one_title = item_one_title;
    }

    @Override
    public int getCount() {
        return item_one_title.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if(view==null){
            view=View.inflate(context, R.layout.item_find_list_grid,null);
            holder=new ViewHolder();
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }
        holder.mLinear= (LinearLayout) view.findViewById(R.id.mLinear);
        holder.item_title = (TextView) view.findViewById(R.id.item_title);
        holder.item_icon =(ImageView) view.findViewById(R.id.item_icon);
        holder.ll =(LinearLayout) view.findViewById(R.id.ll);



        holder.item_icon.setImageResource(item_one_icon.get(i));
        holder.item_title.setText(item_one_title.get(i));

        holder.mLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (i){
                    case 0:
                      Log.e("TAG",item_one_title.get(i));
                        String to = SharedPrefsUtil.getValue(context, "wallet", "token", "");
                        reqequestManager.httpPost(Api.BaseUrl + Api.User_Information, Api.getUserInformation(to, ""), new IRequestCallback() {
                            @Override
                            public void onSuccess(String Response) {
                                Gson gson = new Gson();
                                tokenBean = gson.fromJson(Response, TokenBean.class);
                                photo = tokenBean.getData().getMobile_phone_status();
                                verifiedstatus = tokenBean.getData().getVerified_status();
                                exchange_password = tokenBean.getData().getExchange_password();

                                if (!"1".equals(photo) | verifiedstatus != 2){
                                    //绑定手机号   popup_binding_phone.xml
                                    if (!"1".equals(photo)){
                                        Toast.makeText(context,R.string.Please_bind_your_cell_phone_number_first,Toast.LENGTH_LONG).show();
                                        Intent i = new Intent(context, ChangNumberActivity.class);
                                        i.putExtra("context",2);
                                        i.putExtra("num","");
                                        startActivity(i);
                                    }
                                    if(verifiedstatus != 2){//verifiedstatus 0未认证   1等待审核   2已认证
                                        if (verifiedstatus == 0){
                                            //弹出未实名认证对话框
                                            Toast.makeText(context,R.string.Authentication_by_real_name_please,Toast.LENGTH_LONG).show();
                                            startActivity(new Intent(context, RealAuthenticationAcitivity.class));
                                        }else if(verifiedstatus == 1){
                                            Toast.makeText(context,R.string.Please_wait_in_the_real,Toast.LENGTH_LONG).show();
                                        }else{
                                            Toast.makeText(context,"verifiedstatus = "+verifiedstatus,Toast.LENGTH_SHORT).show();
                                        }

                                    }
                                }

                                if (!"".equals(photo) && verifiedstatus == 2)
                                if(exchange_password != 1){
                                    startActivity(context, SetTradePasswordActivity.class);
                                }else if (exchange_password == 1){
                                    startActivity(context,KKBankActivity.class);
                                }

                            }
                            @Override
                            public void onFailure(String error) {
                                Toast.makeText(context,error,Toast.LENGTH_SHORT).show();
                            }
                        });

//                        photo = "15175226228";
//                        verifiedstatus = 2;
//                        exchange_password = 1;
//                        if (!"".equals(photo) && verifiedstatus == 2 && exchange_password == 1){
//                            Intent intent=new Intent(context,KKBankActivity.class);
//                            context.startActivity(intent);
//                        }
                        break;
                    case 1:
                        Log.e("TAG",item_one_title.get(i));
                        startActivity(context,CoinTransactionActivity.class);
                        break;
                    case 2:
                        Log.e("TAG",item_one_title.get(i));
                        startActivity(context,OffSiteTransactionsActivity.class);
                        break;
                }
            }
        });

        return view;
    }

    public class ViewHolder {
        LinearLayout mLinear,ll;
        ImageView item_icon;
        TextView item_title;
    }
}
